<?php

class MemberController extends BaseController{
    
    public function loginAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $post = $this->input('post','info');
            $validate = array(
                array('email','require','email>'.L('_email_required')),
                array('email','is_email','email>'.L('_email_err'),1,'function'),
                array('pwd','require','pwd>'.L('_pwd_err')),
            );
            $check_result = $MemberModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberModel->getError());
                $this->ajaxReturn($json);
            }else{
                $where['email'] = trim($post['email']);
                $info = $MemberModel->where($where)->find();
                if (encrypt($post['pwd'])!==$info['pwd']) {
                    MemberModel::log($info['member_id'],'用户名或者密码错误',0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_pwd_err')));
                }else{
                    if($info['auth_level']==0){
                        MemberModel::log($info['member_id'],'登录成功',1);
                        $MemberModel->setLogin($info['member_id']);
                        $this->ajaxReturn(array('err'=>0,'msg'=>L('_logn_success'),'action'=>'redirect','url'=>url('ucenter/index')));
                    }else{
                        if($info['auth_level']==1 || $info['auth_level']==3){
                            //发送邮件
                            $res = $this->send_email(trim($post['email']),'login',L('login_email_title'),'login_email_html');
                            if($res['err']==0){
                                $this->ajaxReturn(array('err'=>2,'msg'=>$res['msg'],'auth_level'=>$info['auth_level']));
                            }else{
                                $this->ajaxReturn(array('err'=>1,'msg'=>$res['msg'],'auth_level'=>$info['auth_level']));
                            }
                        }
                        $this->ajaxReturn(array('err'=>2,'msg'=>L('_logn_success'),'auth_level'=>$info['auth_level']));
                    }
                }
            }
        }else{
            $this->show('Member/login.php');
        }
    }

    public function regAction(){
        if(IS_POST){
            $Rsa = new Rsa();
            $code = Cookie::getInstance()->get(md5('reg'));
            $decode = $Rsa->privDecrypt($code);
            $decode = json_decode($decode,true);
            if(time()-$decode['time']>180){
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_code_timeout')));
            }
            $MemberModel = new MemberModel();
            $post = $this->input('post','info');
            $post['checkcode'] =  $decode['code'];
            $validate = array(
                array('protocol',array(1),'protocol>请选阅读注册协议并同意',3,'in'), 
                array('email','require','email>'.L('_email_required')),
                array('email','Validate::isEmail','email>'.L('_email_required'),0,'function'), 
                array('email','','email>'.L('_email_unique'),0,'unique',1),
                array('nick','require','nick>'.L('_nickname_required')),
                array('pwd','require','pwd>'.L('_pwd_required')),
                array('repwd','pwd','repwd>'.L('_pwd_repwd_diff'),0,'confirm'),
                array('code','require','code>'.L('_code_required')),
                array('checkcode','code','code>'.L('_code_err'),0,'confirm'),
            );
            $result = $MemberModel->validate($validate)->create($post);
            if (!$result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberModel->getError());
                $this->error($json['msg']);
            }else{
                $post['pwd'] = encrypt($post['pwd']);
                $post['reg_ip'] = get_client_ip();
                $post['reg_time'] = time();
                $result = $MemberModel->add($post);
                Cookie::getInstance()->del(md5('reg'));
                if ($result) {
                    $MemberModel->setLogin($info['member_id']);
                    MemberModel::log($result,'注册成功',1);
                    $this->success(L('_reg_success'),url('index/index'));
                }else{
                    $this->error(L('_reg_err'));
                }
            }
        }else{
            $this->show('Member/reg.php');
        }
    }

    public function authAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $post = $this->input('post','info');
            $validate = array(
                array('email','require','email>'.L('_email_required')),
                array('email','Validate::isEmail','email>'.L('_email_err'),1,'function'),
                array('pwd','require','pwd>'.L('_pwd_required')),
            );
            if($post['auth_level']==1 || $post['auth_level']==3){
                $validate[] = array('email_code','8','email_code>'.L('_email_code_required'),1,'length');
            }
            if($post['auth_level']==2 || $post['auth_level']==3){
                $validate[] = array('google_code','6','google_code>'.L('_email_code_required'),1,'length');
            }

            $check_result = $MemberModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberModel->getError());
                $this->ajaxReturn($json);
            }else{
                $where['email'] = trim($post['email']);
                $info = $MemberModel->where($where)->find();
                if (encrypt($post['pwd'])!==$info['pwd']) {
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_name_pwd_err')));
                }else{
                    //判断用户验证等级
                    if($info['auth_level']!=$post['auth_level']){
                        MemberModel::log($info['member_id'],'登录失败，非法提交',0);
                        $this->ajaxReturn(array('err'=>1,'msg'=>L('_logn_err'),'action'=>'reload'));
                    }
                    if($info['auth_level']==0){
                        $r[] = true;
                    }
                    if($info['auth_level']==1 || $info['auth_level']==3){
                        $Rsa = new Rsa();
                        $code = Cookie::getInstance()->get(md5('login'));
                        $decode = $Rsa->privDecrypt($code);
                        $decode = json_decode($decode,true);
                        if(time()-$decode['time']>180){
                            $r[] = false;
                        }
                        if($decode['code']!==$post['email_code']){
                            $r[] = false;
                        }
                    }
                    if($info['auth_level']==2 || $info['auth_level']==3){
                        Yaf\loader::import(APP_PATH.'/common/lib/GoogleAuthenticator.php');//公共方法
                        $ga = new PHPGangsta_GoogleAuthenticator();
                        $r[] = $ga->verifyCode($info['auth'], $post['google_code'], 2);
                    }
                    if(!in_array(false,$r)){
                        $MemberModel->setLogin($info['member_id']);
                        MemberModel::log($info['member_id'],'登录成功',1);
                        $this->ajaxReturn(array('err'=>0,'msg'=>L('_logn_success'),'action'=>'redirect','url'=>url('ucenter/index')));
                    }else{
                        MemberModel::log($info['member_id'],'登录失败',0);
                        $this->ajaxReturn(array('err'=>1,'msg'=>L('_logn_err'),'action'=>'reload'));
                    }
                }
            }
        }
    }

    public function send_reg_emailAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $email = $this->input('post','email');
            if(!Validate::isEmail($email)){
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_email_err')));
            }
            $count = $MemberModel->where(array('email'=>$email))->count();
            if($count>0){
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_email_unique')));
            }
            $res = $this->send_email($email,'reg',L('reg_email_title'),'reg_email_html');
            if($res['err']==0){
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_send_success')));
            }else{
                $this->ajaxReturn(array('err'=>1,'msg'=>$res['msg']));
            }
        }
    }

    public function loginoutAction(){
        $MemberModel = new MemberModel();
        $MemberModel->loginOut();
        redirect(url('index/index'));
    }
}